/*
 * 
 */
package utility;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;
import dbmanager.DBManager;

public class MessageGenerator1 
{
	DBManager database;
	
	public MessageGenerator1(DBManager database)
	{
		this.database = database;
	}
	public String convertToMessage(String tablename, String process, Hashtable<Object,Object> hash)
	{
	hash = ConvertToLower.convertHashKey(hash);
		String message="no$";
		StringBuffer  strbuff;
		StringBuffer strbuff_modified = new StringBuffer();
		try
		{
			strbuff = new StringBuffer ();
			//System.out.println ("Index of * : "+indexOfStar);
			
			ResultSet rs = database.getSelect("Select * from command where msgtype='"+tablename+"' and process = '"+process+"' and order1>0 order by order1");
			String Lastspchar = "";
			while (rs.next())
			{
				String position = rs.getString("beforeafter");
				String fieldName = rs.getString("fieldname").toLowerCase();
				String spchar = rs.getString("spchar");
				int plusminus = Integer.parseInt(rs.getString("plusminus"));
				int width = Integer.parseInt(rs.getString("width"));
				
				//System.out.println (" Where to put new transaction_type ? : "+position+" *");
				//System.out.println (" What is the field : "+fieldName);
				
				try 
				{
					String field = hash.get(fieldName).toString();
					if(field.equals("no$"))
						continue;
					if(Lastspchar.equals(spchar))
					{
						if(plusminus==0&&width==0)
						{
							if (position.equals("after"))
								// now replace the string with new transaction type
								strbuff_modified = strbuff.append("\""+field+"\"");
							else if(position.equals("before"))
								strbuff_modified = strbuff.append("\""+field+"\"");
						}
						else
						{
							if (position.equals("after"))
								// now replace the string with new transaction type
								strbuff_modified = strbuff.append(field);
							else if(position.equals("before"))
								strbuff_modified = strbuff.append(field);
						}
					}
					else
					{
						if(plusminus==0&&width==0)
						{
							if (position.equals("after"))
								// now replace the string with new transaction type
								strbuff_modified = strbuff.append(spchar+"\""+field+"\"");
							else if(position.equals("before"))
								strbuff_modified = strbuff.append("\""+field+"\""+spchar);
						}
						else
						{
							if (position.equals("after"))
								// now replace the string with new transaction type
								strbuff_modified = strbuff.append(spchar+field);
							else if(position.equals("before"))
								strbuff_modified = strbuff.append(field+spchar);
						}
					}
					
				} 
				catch (Exception e) 
				{
					continue;
				}
				Lastspchar = spchar;
			}
			message=strbuff_modified.toString();
			System.out.println (message);
		}
		catch (Exception e){
			e.printStackTrace();
		}
		return message;
	} 
	public String messagegerat(String tablename,Hashtable<Object, Object >hash)
	{
		hash = ConvertToLower.convertHashKey(hash);
		StringBuffer strbuff_modified = new StringBuffer();
		StringBuffer strbuff = new StringBuffer();
		Enumeration keyenu = hash.keys();
		String result[][]=null;
		int i=0;
		ResultSet rs = database.getSelect("select fieldname,beforeafter,spchar  from command where msgtype='"+tablename+"' and order1>0 order by order1");
		try {
			rs.last();
			result = new String[rs.getRow()][2];
			rs.first();
			do
			{	
				result[i][0] = rs.getString("spchar");
				result[i][1] = rs.getString("beforeafter");
				i++;
			}while(rs.next());
			
		} catch (SQLException e1) 
		{
			e1.printStackTrace();
		}
		while(keyenu.hasMoreElements())
		{	
			String key = keyenu.nextElement().toString();
			for(int j=0;j<2;j++)
			{
				String spchar = result[j][0];	
				String position = result[j][1];
				if (position.equals("after"))
				{
					strbuff_modified = strbuff.append(spchar+"\""+key+"\"");
				}
				else if(position.equals("before"))
				{
					strbuff_modified = strbuff.append("\""+key+"\""+spchar);
				}
				try 
				{	
					key = hash.get(key).toString();
					
				} catch (RuntimeException e) 
				{
				}
			}
		}
		System.out.println("the string is :"+strbuff_modified.toString());
		return strbuff_modified.toString();
	}
	
	public String messagegerat(String tablename,Hashtable<Object, Object >hash,String process)
	{
		hash = ConvertToLower.convertHashKey(hash);
		StringBuffer strbuff_modified = new StringBuffer();
		StringBuffer strbuff = new StringBuffer();
		Enumeration keyenu = hash.keys();
		String result[][]=null;
		int i=0;
		ResultSet rs = database.getSelect("select fieldname,beforeafter,spchar  from command where msgtype='"+tablename+"' and process ='"+process+"' and order1>0 order by order1");
		try {
			rs.last();
			result = new String[rs.getRow()][2];
			rs.first();
			do
			{	
				result[i][0] = rs.getString("spchar");
				result[i][1] = rs.getString("beforeafter");
				i++;
			}while(rs.next());
			
		} catch (SQLException e1) 
		{
			e1.printStackTrace();
		}
		while(keyenu.hasMoreElements())
		{	
			String key = keyenu.nextElement().toString();
			for(int j=0;j<2;j++)
			{
				String spchar = result[j][0];	
				String position = result[j][1];
				if (position.equals("after"))
				{
					strbuff_modified = strbuff.append(spchar+"\""+key+"\"");
				}
				else if(position.equals("before"))
				{
					strbuff_modified = strbuff.append("\""+key+"\""+spchar);
				}
				try 
				{	
					key = hash.get(key).toString();
					
				} catch (RuntimeException e) 
				{
				}
			}
		}
		System.out.println("the string is :"+strbuff_modified.toString());
		return strbuff_modified.toString();
	}
	
	public String MasterMessage(String tablename,Hashtable<Object, Object >hash, String process)
	{
		hash = ConvertToLower.convertHashKey(hash);
		String Request="";
		ResultSet rs = database.getSelect("select * from command where msgtype='"+tablename+"' and process ='"+process+"' and order1>0 order by order1");
		
		try
		{
			while(rs.next())
			{
				try
				{
					String col = rs.getString("fieldname").toLowerCase();
					col = "\""+ hash.get(col).toString() + "\"";
					String beforeafter = rs.getString("beforeafter");
					if(beforeafter.toLowerCase().equals("after"))
						Request = Request+ rs.getString("spchar") + col;
					if(beforeafter.toLowerCase().equals("before"))
						Request =  col + rs.getString("spchar") + Request ;
				} 
				catch (Exception e) {
				}
			}
		} 
		catch (SQLException e) {
			e.printStackTrace();
		}
		return Request;
	}
	
	public String MasterMessage(String tablename,Hashtable<Object, Object >hash)
	{
	hash = ConvertToLower.convertHashKey(hash);
		String Request="";
		ResultSet rs = database.getSelect("select * from command where msgtype='"+tablename+"' and order1>0 order by order1");
		try
		{
			while(rs.next())
			{
				try
				{
					String col = rs.getString("fieldname").toLowerCase();
					col = "\""+ hash.get(col).toString() + "\"";
					String beforeafter = rs.getString("beforeafter");
					if(beforeafter.toLowerCase().equals("after"))
						Request = Request+ rs.getString("spchar") + col;
					if(beforeafter.toLowerCase().equals("before"))
						Request =  col + rs.getString("spchar") + Request ;
				} 
				catch (Exception e) {
				}
			}
		} 
		catch (SQLException e) {
			e.printStackTrace();
		}
		return Request;
	}
	public String messagegerat(String tablename,String []hash)
	{		
		StringBuffer strbuff_modified = new StringBuffer();
		StringBuffer strbuff = new StringBuffer();
		//Enumeration keyenu = hash.keys();
		String result[][]=null;
		int i=0;
		ResultSet rs = database.getSelect("select fieldname,beforeafter,spchar  from command where msgtype='"+tablename+"' order by order1");
		try {
			rs.last();
			result = new String[rs.getRow()][2];
			rs.first();
			do
			{	
				result[i][0] = rs.getString("spchar");
				result[i][1] = rs.getString("beforeafter");
				i++;
			}while(rs.next());
			
		} catch (SQLException e1) 
		{
			e1.printStackTrace();
		}
		for(int k=0;k<hash.length;k++ )
		{	
			String line = hash[k];
			String splitline[] = line.split("\t");
			String key = splitline[0]; 
			for(int j=0;j<2;j++)
			{
				String spchar = result[j][0];	
				String position = result[j][1];
				if (position.equals("after"))
				{
					strbuff_modified = strbuff.append(spchar+"\""+key+"\"");
				}
				else if(position.equals("before"))
				{
					strbuff_modified = strbuff.append("\""+key+"\""+spchar);
				}
				try 
				{	
					key = splitline[1];
					
				} catch (RuntimeException e) 
				{
				}
			}
		}
		System.out.println("the string is :"+strbuff_modified.toString());
	return strbuff_modified.toString();
	}
	public static void main(String[] args) 
	{
		DBManager db = new DBManager();
		db.getConnect();
		MessageGenerator1 mg = new MessageGenerator1(db);
		Hashtable< Object, Object>hash1 =new Hashtable<Object, Object>();
		
		Hashtable< Object, Object>hash =new Hashtable<Object, Object>();
		
		hash1.put("name","Emp1");
		hash.put("Empname","Hemant");
		hash.put("City","Pune");
		hash.put("Mobile","+919922918606");
		hash.put("pincode","562636");
		//Vector<Object>vector= new Vector<Object>();
		//vector.add("Conditionoperator\t=");
		//System.out.print(mg.messagegerat("property_details",hash,"add"));
		String message = mg.MasterMessage("master_table",hash1);
        System.out.println("The message is "+message);
		//System.out.print("Master Message is "+mg.MasterMessage("master_table",hash1,"add"));
		message = message + mg.messagegerat("property_details",hash);
		//System.out.print(mg.messagegerat("property_details",hash,"add"));
		System.out.println("Message is "+message);
	}
}
